package com.group.yqxxb.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.group.yqxxb.Exception.MyException;
import com.group.yqxxb.service.FriendService;
import com.group.yqxxb.service.UserService;
import com.group.yqxxb.util.ApiUtil;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;


@Service
public class FriendServiceImpl implements FriendService {
    ApiUtil apiUtil = new ApiUtil();

    //添加好友
    @Override
    public void AddFriend(String accid,String faccid,Integer type,String msg) throws Exception{
        HttpPost httpPost = apiUtil.BuildHeader("https://api.netease.im/nimserver/friend/add.action");
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("accid",accid));
        params.add(new BasicNameValuePair("faccid",faccid));
        if ( type != null){
            params.add(new BasicNameValuePair("type",type.toString()));
        }
        params.add(new BasicNameValuePair("msg",msg));
        httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8"));
        JSONObject jsonObject = apiUtil.ApiResponse(httpPost);
        if (jsonObject.getInteger("code") == 200){
            return;
        }else{
            throw new MyException(jsonObject);
        }
    }

    //更新好友信息
    @Override
    public void UpdateFriend (String accid,String faccid,String alias,String ex) throws Exception{
        HttpPost httpPost = apiUtil.BuildHeader("https://api.netease.im/nimserver/friend/update.action");
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("accid",accid));
        params.add(new BasicNameValuePair("faccid",faccid));
        if(alias != null){
            params.add(new BasicNameValuePair("alias",alias));
        }
        if (ex !=  null){
            params.add(new BasicNameValuePair("ex",ex));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8"));
        JSONObject jsonObject = apiUtil.ApiResponse(httpPost);
        if (jsonObject.getInteger("code") == 200){
            return;
        }else{
            throw new MyException(jsonObject);
        }
    }

    //删除好友
    @Override
    public void DeleteFriend (String accid,String faccid ,Boolean isDeleteAlias) throws Exception{
        HttpPost httpPost = apiUtil.BuildHeader("https://api.netease.im/nimserver/friend/delete.action");
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("accid",accid));
        params.add(new BasicNameValuePair("faccid",faccid));
        if(isDeleteAlias != null){
            params.add(new BasicNameValuePair("isDeleteAlias",isDeleteAlias.toString()));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8"));
        JSONObject jsonObject = apiUtil.ApiResponse(httpPost);
        if (jsonObject.getInteger("code") == 200){
            return;
        }else{
            throw new MyException(jsonObject);
        }
    }

    //获取好友关系
    @Override
    public JSONObject GetFriend(String accid) throws Exception{
        HttpPost httpPost = apiUtil.BuildHeader("https://api.netease.im/nimserver/friend/get.action");
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("accid",accid));
        params.add(new BasicNameValuePair("updatetime",String.valueOf((new Date()).getTime()/1000L)));
        httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8"));
        JSONObject jsonObject = apiUtil.ApiResponse(httpPost);
        if (jsonObject.getInteger("code") == 200){
            return jsonObject;
        }else{
            throw new MyException(jsonObject);
        }
    }

    //设置黑名单或设置静音
    @Override
    public void SetSpecialRelation(String accid,String targetAcc,Integer relationType,Integer value) throws Exception{
        HttpPost httpPost = apiUtil.BuildHeader("https://api.netease.im/nimserver/user/setSpecialRelation.action");
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("accid",accid));
        params.add(new BasicNameValuePair("targetAcc",targetAcc));
        params.add(new BasicNameValuePair("relationType",relationType.toString()));
        params.add(new BasicNameValuePair("value",value.toString()));
        httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8"));
        JSONObject jsonObject = apiUtil.ApiResponse(httpPost);
        if (jsonObject.getInteger("code") == 200){
            return;
        }else{
            throw new MyException(jsonObject);
        }
    }
}
